package quickstart;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

/**
 * Created by huangyu on 2018/9/22 23:59
 */
public class Consumer {

	public static void main(String[] args) throws Exception {
		// 1.创建一个连接工厂，并配置连接地址
		ConnectionFactory connectionFactory = new ConnectionFactory();
		connectionFactory.setHost("192.168.177.128");
		connectionFactory.setPort(5672);
		connectionFactory.setVirtualHost("/");
		// 2.创建连接
		Connection connection = connectionFactory.newConnection();
		// 3.创建一个通道
		Channel channel = connection.createChannel();

		// 4.声明队列
		channel.queueDeclare("test001", true, false, false, null);
		// 5.创建消费者
		QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
		// 6.设置channel
		channel.basicConsume("test001", true, queueingConsumer);

		// 7.获取消息
		while (true) {
			QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
			String msg = new String(delivery.getBody());
			System.out.println("收到的消息: " + msg);
		}

	}
}
